﻿
var nosend=false;var conversation=loadTimer=null; var val = '';
var nav=navigator.appName;
var isIE=(nav.indexOf('Microsoft')>=0)?true:false;
var isMz=(nav=='Netscape')?true:false;
var isOp=(nav=='Opera')?true:false;

function rel(id){
  return document.getElementById (id)||null;
}
function setHTML(el,sHTML){
  if(rel(el)){
   rel(el).innerHTML=sHTML;
  }
  else{
    alert('The element "'+el+'" not found !');
  }
}

//var RTE=null;
//function init(sRteID,sSpanID,sIniText){
//  //var txt='<a href="http://www.informationweek.com/story/showArticle.jhtml?articleID=205204433&amp;cid=RSSfeed_IWK_All" target="_new">AOL Kills Netscape\'s Future, Leaves Firefox To Battle IE</a>&nbsp;  <br>&nbsp;<br>Development on the browser had recently devolved into a handful of engineers tasked with creating a skinned version of <span style="font-weight:bold;">Firefox</span> with a few extensions, AOL said.';
//  RTE=new RichTextEditor(sRteID,sSpanID,sIniText, 500, 240);
//  setActionOnButton(sRteID+'btnBold', sRteID+'.format("bold")');
//  setActionOnButton(sRteID+'btnItalic', sRteID+'.format("italic")');
//  setActionOnButton(sRteID+'btnUnderline', sRteID+'.format("underline");');
//  setActionOnButton(sRteID+'btnInsertImage', sRteID+'.insertImage();');
//  setActionOnButton(sRteID+'btnAddLink', sRteID+'.addLink();');
//  setActionOnButton(sRteID+'btnUnformat', sRteID+'.unformat();');
//  setActionOnButton(sRteID+'btnCut', sRteID+'.useClipboard("cut");');
//  setActionOnButton(sRteID+'btnCopy', sRteID+'.useClipboard("copy");');
//  setActionOnButton(sRteID+'btnPaste', sRteID+'.useClipboard("paste");');
//  setActionOnButton(sRteID+'btnSave', sRteID+'.doPost();');//'javascript:alert(RTE.getContent());');
//}

function RichTextEditor(sID, oContain, sDefaultText, iWidth, iHeight){
  this.ID=sID;
  this.content=sDefaultText;
  this.width= (iWidth>300?iWidth:700);
  this.height=(iHeight>80?iHeight:200);
  this.clipBoard='';
   
    var sEditor='<iframe id="'+this.ID;
        sEditor+='" style="width:96%;';//+this.width;
        sEditor+='height:'+this.height;
	    sEditor+='px;overflow:auto;"></iframe>';
          setHTML(oContain,sEditor);
  
  this.UI=document.getElementById(this.ID).contentWindow;
  this.editor=this.UI.document;

    with(this.editor){
      designMode='On';
      open();  
	  write('<html><head></head><body>'+this.content+'</body></html>');
      close();
    }
}
RichTextEditor.prototype.format=function(command, value){
  if(this.editor.queryCommandEnabled(command)){
	if(!value){value=null;}
		this.UI.focus();
		this.editor.execCommand(command,false,value);
  }
}
RichTextEditor.prototype.addLink=function(){
  var aLink=prompt('Enter or paste a link :', '');
	if(aLink){
		this.format('CreateLink',  aLink);
	}
}
RichTextEditor.prototype.insertImage=function(){
  var aLink=prompt('Enter or paste a URL :', '');
	if(aLink){
		this.format('InsertImage',  aLink);
	}
}
RichTextEditor.prototype.useClipboard=function(command){
  if(isIE){
    this.format(command);
  }   
  else{
    if(command=='cut'||command=='copy'){
      var sel = this.UI.getSelection();
      if(sel==''){return;}
      var range = sel.getRangeAt(0);
      var docFrag = (command=='cut')?range.extractContents():range.cloneContents();
      var xmls = new XMLSerializer();
      this.clipBoard=xmls.serializeToString(docFrag);   
    }
    else if(command=='paste'){
      if(this.clipBoard!=''){
        this.format('inserthtml',this.clipBoard);
      }
    }
  }
  this.UI.focus();
}

RichTextEditor.prototype.unformat=function(){
  if(isOp){
	var sel = this.UI.getSelection();
	this.format('inserthtml', sel);
  }
  else{
	this.format('removeformat');
	this.format('unlink');
  }
}
RichTextEditor.prototype.getContent=function(){
	//return this.editor.body.innerHTML;
	//mstr_editorContext = this.editor.body.innerHTML;//myContext ;
	//document.frmMainAdmin.RTEContext.value=  this.editor.body.innerHTML;
	//SetCookie("cookContext",  this.editor.body.innerHTML );
	return this.editor.body.innerHTML;//mstr_editorContext
}
function setActionOnButton(elementId, Action){ 
   var el = rel(elementId); 
    if(el.addEventListener){
  	  el.addEventListener('click', new Function(Action), false); 
    } 
    else if (el.attachEvent){
  	  el.attachEvent('onclick', new Function(Action));   
    }   
}  
RichTextEditor.prototype.doReset=function(){
	this.editor.body.innerHTML='';
	clearTxt('status');
}
function clearTxt(obj){try{gTxt(obj,"");}catch(e){}}
RichTextEditor.prototype.getText=function(){
  return isIE?this.editor.body.innerText:this.editor.body.textContent;
}
function stripNode(node){
  if(node.indexOf('<span id="conversationList">')!=-1){
	node=node.replace('<span id="conversationList">','');
	node=node.substring(0,node.length-7);
  }
  return node;
}
function strip_tag(str){return str.replace(/<(?:.|\s)*?>/g, "");}
function cod(s){return encodeURIComponent(s);}

function AJAX_Control(){
  var req=null;
    if(window.ActiveXObject){
      req=new ActiveXObject("Msxml2.XMLHTTP");
        if(!req){
          req=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    else if(window.XMLHttpRequest){
      req=new XMLHttpRequest();
      req.overrideMimeType('text/html');
    }
  return req;
} 

    RichTextEditor.prototype.doPost=function(){
    // if(author==null){
    //	author=prompt("Xin vui lòng cho biết quý danh !","");
    //	if(!author){hide("step2");hide("step3");show("step1");return;}
    //	author=cod(author);
    //	setSNLabsCookie('sender',author);
    // }
 var body=this.getContent();
  if(body==''){
	alert("Xin lỗi, bạn muốn gửi đi thứ gì ?!");
	return;
  }
  else if(this.getText().length<20){
	alert("Xin lỗi, thông điệp của bạn quá ngắn !");
	return;
  }
  else{
	if(body.length>10000){
	  var q=confirm("Thông điệp vượt quá giới hạn 10 ngàn ký tự.\n\nBạn có muốn hiệu chỉnh lại không ?");
	  if(q){return;}
	  body=body.substring(0,10000);
	}
   if(nosend==false){
	nosend=true;
	setHTML('status','<font color="blue">Sending...</font>');
	val='content='+ cod(body);
	var req=AJAX_Control();
    if(req!=null&&typeof(req)!=undefined){
      req.onreadystatechange =function(){
        if(req.readyState==4&&req.status==200){
		  var res=strip_tag(req.responseText);var k=0;
			if(res.indexOf('Added')>=0){k=1;}
			else if(res.indexOf('Updated')>=0){k=0;}
			else if(res.indexOf('No subject')>=0){k=-1;}
			else if(res.indexOf('incomplete')>=0){k=-2;}
		    switch(k){
				case 0 : RTE.doReset();break;
				case 1 : clearTxt('status');RTE.doReset();alert('Thông điệp đã được gửi đi thành công.');getxml();hide("step2");hide("step3");show("step1");break;
				case -1 : setHTML('status','<font color="red">Có lỗi xảy ra khi gửi dữ liệu</font>');onTop();break;				
				case -2 : setHTML('status','<font color="red">Đã xảy ra lỗi trong quá trình gửi dữ liệu. <span class="rLabel" onclick="RTE.doPost();" title="Thử lại">Thử lại</span>.</font>');onTop();break;			
				default : nosend=false;setHTML('status','');
			}
			nosend=false;
        }
      }
	req.open("POST", '../RTESend.aspx', true);          
	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	req.send(val);
    }  
  }
 }
} 

function SetCookie(sName, sValue)
{
    date = new Date();
    //  document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
    var the_cookie = sName;// + "=" + sValue  // + "; expires=" + date.toGMTString();
    document.cookie = the_cookie + '=' + escape(sValue);
    alert(GetCookie(sName));
    //  alert(escape(sValue));
}

// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

// Delete the cookie with the specified name.
function DelCookie(sName)
{
  document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

//window.onload=init;